<?php

/**
 * This is the model class for table "professor".
 */
class Professor extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'professor':
	 * @var integer $id
	 * @var string $lastName
	 * @var string $firstName
	 * @var string $middleName
	 * @var string $email
	 * @var integer $department_id
	 * @var string $password
         *
	 */
    /**
	 * Returns the static model of the specified AR class.
	 * @return Professor the static model class

     */

    

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'professor';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('lastName, firstName, email, department_id, password', 'required'),
			array('department_id', 'numerical', 'integerOnly'=>true),
			array('lastName, firstName, middleName', 'length', 'max'=>50),
			array('email', 'length', 'max'=>70),
			array('password', 'length', 'max'=>45),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, lastName, firstName, middleName, email, department_id, password', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'courseworks' => array(self::HAS_MANY, 'Courseworks', 'professor_id'),
			'department' => array(self::BELONGS_TO, 'Department', 'department_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'lastName' => 'Фамилия',
			'firstName' => 'Имя',
			'middleName' => 'Отчество',
			'email' => 'Email',
			'department_id' => 'Кафедра',
                        'faculty_id' => 'Факультет',
			'password' => 'Пароль',
                    'fio'=>'ФИО'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('lastName',$this->lastName,true);

		$criteria->compare('firstName',$this->firstName,true);

		$criteria->compare('middleName',$this->middleName,true);

		$criteria->compare('email',$this->email,true);

		$criteria->compare('department_id',$this->department_id);


		$criteria->compare('password',$this->password,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

        public function validatePassword($password) {
            return $this->hashPassword($password,Yii::app()->params['salt'])===$this->password;
        }
        
        public function hashPassword($password,$salt) {
            return md5($salt.$password);
        }

   /*     public function randomSalt($length=32)
        {
        $chars = "abcdefghijkmnopqrstuvwxyz023456789";
        srand((double)microtime()*1000000);
        $i = 1;
        $salt = '' ;

        while ($i <= $length)
        {
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $salt .= $tmp;
            $i++;
        }
        return $salt;
    }

    */
        public function getFaculty() {
            return $this->getAttribute($this->department);
        }



          public function getAllFaculty()
        {
             $models = faculty::model()->findAll();
             $list=CHTML::ListData($models,'id','name');
             return $list;
        }

         public function getAllDepartment()
        {
             $models = department::model()->findAll();
             $list=CHTML::ListData($models,'id','name');
             return $list;
        }
       
}